<?php

/**
 * generates sorting links for the listAction() view script
 *
 * @author    Mon Zafra <monzee at gmail>
 * @copyright (c)2009 Mon Zafra
 * @license   http://monzee.wordpress.com/license New BSD
 * @version   $Id: SortLink.php 38 2009-02-18 17:12:39Z monzee $
 */
class Crud_SortLink
{
    protected $_view;

    public function setView($view)
    {
        $this->_view = $view;
    }

    public function sortLink($colName)
    {
        $params = Zend_Controller_Front::getInstance()->getRequest()->getParams();
        $view = $this->_view;

        $url = array('sort' => $colName, 'order' => null);
        if (isset($params['sort']) && $params['sort'] == $colName) {
            if (!isset($params['order']) || strtolower($params['order']) != 'desc') {
                $url['order'] = 'desc';
            }
        }
        $col = $view->inflect($colName)->toTitle();
        $ret = '<a href="' . $view->url($url) . '" title="Sort by '. $col . '">'
             . $col
             . '</a>';

        return $ret;
    }

}
